package epidemic

import (
	"gitee.com/baal1990/epidemic/global"
	response "gitee.com/baal1990/epidemic/model/common/response_dfitc"
	"gitee.com/baal1990/epidemic/model/epidemic"
	request2 "gitee.com/baal1990/epidemic/model/epidemic/request"
	epidemic2 "gitee.com/baal1990/epidemic/service/epidemic"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type EpidemicRelatedStaffHistoryApi struct {
}

var historyService epidemic2.EpidemicRelatedStaffHistoryService

// List 历史涉疫人员列表（居家人员明细）
func (e EpidemicRelatedStaffHistoryApi) List(c *gin.Context) {
	epidemicUser := c.MustGet("OPERATION_USER_INFO").(epidemic.EpidemicUser)
	var pageInfo request2.EpidemicRelatedStaffHistorySearch
	_ = c.ShouldBindQuery(&pageInfo)
	global.GVA_LOG.Info("请求参数", zap.Any("param", pageInfo))
	if pageInfo.Name != "" {
		nameDecodeBytes, err := global.AsymmetricCrypto.DecryptWithPrvKeyFromBase64(pageInfo.Name)
		if err != nil {
			global.GVA_LOG.Error("解密失败", zap.Error(err))
			response.FailWithMessage("参数错误", c)
			return
		}
		pageInfo.Name = string(nameDecodeBytes)
	}
	if list, total, err := historyService.List(pageInfo, epidemicUser); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

// Export 导出全部
func (e EpidemicRelatedStaffHistoryApi) Export(c *gin.Context) {
	user := c.MustGet("OPERATION_USER_INFO").(epidemic.EpidemicUser)
	fileName, err := historyService.Export(user)
	if err != nil {
		global.GVA_LOG.Error("导出失败!", zap.Error(err))
		response.FailWithMessage("导出失败", c)
	} else {
		fileLink := global.GVA_CONFIG.System.AccessHost + fileName
		response.OkWithData(fileLink, c)
	}
}

// Reduction 还原
func (e EpidemicRelatedStaffHistoryApi) Reduction(c *gin.Context) {
	req := request2.EpidemicRelatedStaffHistoryOperation{}
	if err := c.ShouldBind(&req); err != nil {
		global.GVA_LOG.Error("参数错误!", zap.Error(err))
		response.FailWithMessage("参数错误", c)
	}
	global.GVA_LOG.Info("请求参数", zap.Any("param", req))
	err := historyService.Reduction(req)
	if err != nil {
		global.GVA_LOG.Error("数据恢复失败!", zap.Error(err))
		response.FailWithMessage("操作失败", c)
	} else {
		response.OkWithMessage("操作成功", c)
	}
}
